// 路由部分
import { BrowserRouter } from 'react-router-dom';

// 引入路由插件
import RouterWaiter from './router/RouterWaiter';
import routes from './router';
import onRouteBefore, { whiteList } from './router/onRouteBefore';

// ui库
import zhCN from 'antd/locale/zh_CN';
import 'dayjs/locale/zh-cn';
import { theme as antdTheme } from 'antd';
import { GenerateRoutes } from '@/hooks/useInfo.hooks';

/**
 * 函数式组件
 * @param {*} props
 * @returns
 */
export default function App() {
	const { token, themeConfig } = useSelector((state) => state.base),
		{ queryRoutes, Routes, overallLoading } = useSelector((state) => state.user);

	// 登录成功 - 生成对应权限的路由
	useEffect(() => {
		if (token && !queryRoutes) GenerateRoutes();
	}, [token, queryRoutes]);

	useEffect(() => {
		// 判断登录 && 没有在免登录白名单，跳转登录页面
		if (!token && !whiteList.includes(location.pathname)) location.assign('/login');
	}, [token, whiteList]);

	return (
		<ConfigProvider locale={zhCN} theme={{ cssVar: true, algorithm: themeConfig.isDark ? antdTheme.darkAlgorithm : antdTheme.defaultAlgorithm }}>
			<Spin spinning={overallLoading} wrapperClassName="w-100% h-100%" size="large">
				{useMemo(
					() => (
						<>
							<BrowserRouter>
								<RouterWaiter routes={[...routes, ...Routes]} onRouteBefore={onRouteBefore} />
							</BrowserRouter>
						</>
					),
					[Routes]
				)}
			</Spin>
		</ConfigProvider>
	);
}
